package com.cc.moxingvoice.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.cc.moxingvoice.MyApplication;
import com.cc.moxingvoice.ui.voice.RecordActivity;

/**
 * 创建日期：2018/2/25 on 17:32
 * 描述：
 * 作者：郭士超
 * QQ：1169380200
 */

public class PermissionsUtil {

    /**
     * 6.0动态读写权限
     */
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };

    /**
     * 6.0动态读写权限
     */
    public static boolean verifyStoragePermissions(Activity activity) {

        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            }else {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 6.0动态悬浮窗权限
     */
    public static boolean showFloatWindow(Context context) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (Settings.canDrawOverlays(context)) {
                return true;
            } else {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                context.startActivity(intent);
                return false;
            }
        } else {

            return true;
        }
    }

    /**
     * 动态录音权限
     * @param activity
     * @return
     */
    public static boolean audioRecordPermissions(Activity activity){
        if(ContextCompat.checkSelfPermission(MyApplication.getInstance(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                int RECORD_AUDIO = 1;
                activity.requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO},
                        RECORD_AUDIO);
            }
            return false;
        }
        return true;
    }

}
